package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.IEmpAddressService;
import com.yt.sys.models.bo.EmpAddressBO;
import com.yt.sys.models.dto.EmpAddressDTO;
import com.yt.sys.models.bo.EmpAddressSaveBO;
import com.yt.sys.models.bo.EmpLocationUpdateBO;

import java.util.List;

/**
 * 员工位置信息控制器
 *
 * @author yzz
 * @Date 2025-09-17 14:29:56
 */

@RestController
@RequestMapping("/api/empAddress")
@Slf4j
@Api(value = "EmpAddressApiController",tags = "员工位置信息Api接口")
public class EmpAddressApiController {

    @DubboReference
    private IEmpAddressService empAddressService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EmpAddressDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        EmpAddressDTO dto = empAddressService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "员工位置信息列表",notes = "员工位置信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<EmpAddressDTO>> queryPageList(EmpAddressBO bo){
        Wrapper<EmpAddressDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<EmpAddressDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = empAddressService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    
    @PostMapping("/updateLocation")
    @ApiOperation(value = "更新员工位置", notes = "更新员工位置信息")
    public Result<String> updateLocation(@RequestBody EmpLocationUpdateBO locationBO) {
        // 参数校验
        if (locationBO.getEmpId() == null) {
            return new Result<>(CodeEnum.FAIL_100002, "员工ID不能为空");
        }
        
        if (locationBO.getLatitude() == null || locationBO.getLongitude() == null) {
            return new Result<>(CodeEnum.FAIL_100002, "经纬度信息不能为空");
        }

        // 调用服务更新位置信息
        boolean success = empAddressService.updateLocation(locationBO);
        
        if (success) {
            return new Result<>(CodeEnum.SUCCESS, "位置更新成功");
        } else {
            return new Result<>(CodeEnum.FAIL_100001, "位置更新失败");
        }
    }

    @PostMapping("/listAll")
    @ApiOperation(value = "获取所有员工位置信息", notes = "获取所有员工位置信息")
    public Result<List<EmpAddressDTO>> listAll() {
        List<EmpAddressDTO> list = empAddressService.selectList(null);
        return new Result<>(list);
    }
}